package com.et.core.file

import android.os.Environment
import android.util.Log
import com.et.core.store.FileEntity
import com.et.core.store.NORMAL
import com.et.logger.ELog
import com.et.logger.TAGS
import com.et.util.download
import com.google.gson.Gson
import com.google.gson.annotations.SerializedName
import io.reactivex.rxjava3.core.Single
import io.reactivex.rxjava3.schedulers.Schedulers
import java.io.File

interface IAdvert {
    fun observer(_callback: () -> Unit)
}

@Suppress("DEPRECATION")
class Advert : IAdvert {

    private var hashSet: HashSet<(() -> Unit)> = HashSet()
    override fun observer(_callback: () -> Unit) {
        hashSet.add(_callback)
    }

    fun addAdvert(json: String) {
        Single.just(json).map { s ->
            Gson().fromJson(s, AdvertGson::class.java).apply {
                Environment.getExternalStoragePublicDirectory("advert").apply {
                    if (!exists()) mkdir()
                    val instance = AdvHelper.getInstance()
                    instance.clear()
                    ELog.d(TAGS.vending, "Adv download finish>>size  ${mediaSetList.size}")
                    mediaSetList.forEach {
                        File(this, it.filename).apply {
                            download(it.url) { f ->
                                if (f == 1f) {
                                    ELog.d(TAGS.vending, "Adv download finish>>${it.filename}")
                                    //添加到数据库
                                    instance.add(
                                        FileEntity(
                                            uid = it.md5.hashCode(),
                                            name = it.filename,
                                            fileUrl = it.url,
                                            fileLength = length(),
                                            filePath = absolutePath,
                                            status = NORMAL
                                        )
                                    )
                                } else {
                                    ELog.d(TAGS.vending, "Adv download update>>$f")
                                }
                            }
                        }
                    }

                }
            }
        }
            .subscribeOn(Schedulers.single())
            .observeOn(Schedulers.single())
            .subscribe({
                Log.d(TAGS.vending, it.toString())
                AdvHelper.getInstance().subscribe()
                if (hashSet.isNotEmpty()) {
                    hashSet.forEach { it() }
                }
            }, { throwable ->
                ELog.d(TAGS.vending, "vending:advert update>>$throwable")
            })
    }

    companion object {
        @JvmStatic
        fun getInstance() = Holder.holder
    }

    object Holder {
        val holder = Advert()
    }
}


data class AdvertGson(
    @SerializedName("play_order_image2") val play_order_image2: String,
    @SerializedName("mediaSetList") val mediaSetList: MutableList<MediaSetList>,
    @SerializedName("type") val type: String,
    @SerializedName("play_order_image") val play_order_image: String,
    @SerializedName("play_order_image_time") val play_order_image_time: Int,
    @SerializedName("play_mode2") val play_mode2: String,
    @SerializedName("volume") val volume: Int,
    @SerializedName("play_order_video") val play_order_video: String,
    @SerializedName("play_order_image_time2") val play_order_image_time2: Int,
    @SerializedName("avm") val avm: String,
    @SerializedName("play_order_image_left") val play_order_image_left: String,
    @SerializedName("play_mode") val play_mode: String,
    @SerializedName("play_order_image_pay") val play_order_image_pay: String,
    @SerializedName("time") val time: String,
    @SerializedName("play_order_video2") val play_order_video2: String,
    @SerializedName("play_order_image_right") val play_order_image_right: String,
) {
    data class MediaSetList(
        @SerializedName("filename") val filename: String,
        @SerializedName("url") val url: String,
        @SerializedName("md5") val md5: String,
    )
}